﻿using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using Syngine.Components;

namespace Syngine.Physics.Components
{
	/// <summary>
	/// 
	/// </summary>
	public abstract class DrawableSprite : Drawable
	{
		/// <summary>
		/// 
		/// </summary>
		public Body Body { get; protected set; }

		/// <summary>
		/// 
		/// </summary>
		public virtual Vector2 DisplayPosition
		{
			get
			{
				if (Body == null)
				{
					return Vector2.Zero;
				}

				return Body.Position.ToDisplayUnits();
			}
		}

		/// <summary>
		/// Gets or sets the display position reletive to the screen.
		/// </summary>
		public override Vector2 Position
		{
			get
			{
				return DisplayPosition;
			}
			set
			{
				Body.Position = value.ToSimUnits();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public virtual Vector2 SimPosition
		{
			get
			{
				if (Body == null)
				{
					return Vector2.Zero;
				}

				return Body.Position;
			}
		}

		/// <summary>
		/// Applies a force at the center of mass.
		/// </summary>
		/// <param name="force"></param>
		public void ApplyForce(Vector2 force)
		{
			if (Body != null)
			{
				Body.ApplyForce(force);
			}
		}

		/// <summary>
		/// Apply a force at a world point. If the force is not applied at the center
		/// of mass, it will generate a torque and affect the angular velocity. This
		/// wakes up the body.
		/// </summary>
		/// <param name="force">The world force vector, usually in Newtons (N).</param>
		/// <param name="point">The world position of the point of application.</param>
		public void ApplyForce(Vector2 force, Vector2 point)
		{
			if (Body != null)
			{
				Body.ApplyForce(force, point);
			}
		}

		/// <summary>
		/// Apply a torque. This affects the angular velocity without affecting
		/// the linear velocity of the center of mass. This wakes up the body.
		/// </summary>
		/// <param name="torque"></param>
		public void ApplyTorque(float torque)
		{
			Body.ApplyTorque(torque);
		}
	}
}